<template>
  <div class="mod-config">
    <basic-container>
      <el-divider content-position="left">查询条件</el-divider>
      <el-form inline size="mini" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="加工日期">
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.beginTime"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
              <el-col class="line" :span="2" style="text-align: center;">至</el-col>
              <el-col :span="11">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="dataForm.endTime"
                  value-format="yyyy-MM-dd"
                  style="width: 100%;"
                ></el-date-picker>
              </el-col>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="margin-top: 10px;">
          <el-form-item label-width="5px">

            <el-link :href="printUrl" target="_blank" type="primary" :disabled="isDisabled">
              <el-button size="mini" type="primary" @click="getAllocationInfoList">查 询</el-button>
            </el-link>
          </el-form-item>
        </el-row>
      </el-form>
      <search-stock v-if="stockVisible" ref="stockInfo" @refreshData="getStockInfoList"></search-stock>
    </basic-container>
  </div>
</template>

<script>
    let params = "";
    import { filterParams, getDateFormat } from "@/util/tools"
    import { mapGetters } from "Vuex"
    import { getUserInfo } from "@/api/admin/user"
    import { getStockList } from '@/api/eims/stock'
    import searchStock from "@/components/business/stock-search"


    export default {
        data() {
            return {
                printUrl: "",
                isDisabled: true,
                outStockId: "",
                inStockId: "",
                dataForm: {
                    beginTime: "",
                    endTime: "",
                },
                deptOptions: [],
                stockOptions: [],
                stockVisible: false
            };
        },
        computed: {
            ...mapGetters(["userInfo", "bizData"])
        },
        components: {
            searchStock
        },
        created() {
            this.getStockOption()
        },
        methods: {
            typeChangeHandle() {
                this.outStockId = ''
                this.inStockId = ''
                this.dataForm.outStockIds = []
                this.dataForm.outStockName = ''
                this.dataForm.inStockIds = []
                this.dataForm.inStockName = ''
            },
            outStockChangeHandle(val) {
                this.stockOptions.forEach(item => {
                    this.dataForm.outStockIds = []
                    if(item.id == val){
                        this.dataForm.outStockName = item.name
                    }

                })
                this.dataForm.outStockIds.push(val)
            },
            inStockChangeHandle(val) {
                this.stockOptions.forEach(item => {
                    this.dataForm.inStockIds = []
                    if(item.id == val){
                        this.dataForm.inStockName = item.name
                    }

                })
                this.dataForm.inStockIds.push(val)
            },
            getAllocationInfoList() {
                if(!this.dataForm.beginTime){
                    this.$message.warning("请出入查询日期范围");
                    return
                }
                if(!this.dataForm.endTime){
                    this.$message.warning("请出入查询日期范围");
                    return
                }
                this.isDisabled = false
                params = "machining/workingCollect.cpt";
                let data = filterParams(this.dataForm);
                let parameter = "";
                for (let key in data) {
                    parameter += "&" + key + "=" + this.dataForm[key];
                }
                this.printUrl = this.bizData.printUrl + params + parameter;

            },
            //根据工号获取员工真实名称
            getRealName(workNumber) {
                if (workNumber == "") {
                    this.dataForm.createUserName = "";
                    this.dataForm.createUser = "";
                } else {
                    getUserInfo(workNumber).then(result => {
                        if (result.data.data) {
                            this.dataForm.createUserName = result.data.data.realName;
                            this.dataForm.createUser = result.data.data.id;
                        } else {
                            this.$message.warning("请输入正确的员工工号");
                        }
                    });
                }
            },
            getStockList() {
                this.stockVisible = true;
                let data = [];
                if(this.dataForm.type == '1'){
                    if (this.dataForm.inStockIds) {
                        data = this.dataForm.inStockIds;
                    }
                }else if(this.dataForm.type == '2'){
                    if (this.dataForm.outStockIds) {
                        data = this.dataForm.outStockIds;
                    }
                }

                console.log(data)

                this.$nextTick(() => {
                    this.$refs.stockInfo.init(data);
                });
            },
            getStockInfoList(data) {
                let name = "";
                data.forEach(result => {
                    if(this.dataForm.type == '1'){
                        this.dataForm.inStockIds.push(result.id)
                    }else if(this.dataForm.type == '2'){
                        this.dataForm.outStockIds.push(result.id)
                    }
                    name = name ? name + "," + result.name : result.name;
                });
                if(this.dataForm.type == '1'){
                    this.dataForm.inStockName = name
                }else if(this.dataForm.type == '2'){
                    this.dataForm.outStockName = name
                }
            },
            getStockOption(){
                getStockList().then(result => {
                    this.stockOptions = result.data.data
                })
            }
        }
    };
</script>

<style lang="scss" scoped>
  .row-bg {
    background-color: #cadee8;
    // padding-left: 15px;
    padding-right: 15px;
    padding-top: 5px;
    margin-bottom: 2px;
  }
</style>
